<template>
  <div>
     <!-- 查看详情弹框 -->
    <el-dialog
      title="查看详情"
      :visible.sync="eventShow"
      width="1200px"
      :before-close="handleCloses"
      :close-on-click-modal="false"
    >
      <div class="sousuojg6_10">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="机构ID">
            <el-input v-model="formInline.oEMName" placeholder="请输入机构ID"></el-input>
          </el-form-item>
          <el-form-item label="机构ID">
            <el-input v-model="formInline.officeUid" placeholder="请输入机构ID"></el-input>
          </el-form-item>
          <el-form-item label="机构名称">
            <el-input v-model="formInline.officeName" placeholder="请输入机构名称"></el-input>
          </el-form-item>
          <el-col class="boxer">
            <el-button @click="reset" class="btn">重置</el-button>
            <el-button style="margin-right: 10px" type="primary" @click="queryAgentInfos" class="btn">搜索</el-button>
          </el-col>
        </el-form>
      </div>

      <p class="shopList"></p>

      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        height="480"
        :header-cell-style="{background:'#DCDCDC',color:'#222'}"
        style="width: 100%"
      >
        <el-table-column type="selection" align="center" min-width="40"></el-table-column>
        <!-- <el-table-column v-if='types' prop="oEMName" align="center" min-width="80" label="OEM" width="180"></el-table-column> -->
        <el-table-column prop="officeUid" align="center" min-width="200" label="机构ID"></el-table-column>
        <el-table-column prop="officeName" align="center" min-width="200" class="officeName" label="机构名称"></el-table-column>
        <el-table-column prop="productNum" align="center" min-width="200" label="产品总数"></el-table-column>
        <el-table-column prop="openNum" align="center" min-width="200" label="开通总数"></el-table-column>
        <el-table-column prop="amtNum" align="center" min-width="200" label="达标总数"></el-table-column>
        <el-table-column prop="rewardAmt" align="center" min-width="200" label="奖励金额"></el-table-column>
        <el-table-column prop="address" align="center" min-width="160" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="DetailsChild(scope.row.activeUid,scope.row.officeUid)" size="mini">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button type="primary" @click="agentInfoExport" class="btnExport">批量导出</el-button>
      <el-pagination
        background
        style="text-align: right;"
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-size="formInline.number"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10,20,30,40, 50]"
        :total="totalNum"
      ></el-pagination>
    </el-dialog>


    <!-- 查看子详情 -->
    <EventDetailsChild :eventChildShow="eventChildShow" :detailsData='detailsData' v-on="eventChildsuccess()"></EventDetailsChild>
  </div>
</template>
<script>
import EventDetailsChild from './eventDetailsChild'
import bus from "../../assets/js/bus";
import https from "../../plugins/https.js";
import qs from 'qs'
export default {
  props: {
    eventShow: {
      type: Boolean,
      default() {
          return false
      }
    },
    activeUid: {
      type: String
    }
  },
  components:{EventDetailsChild},
  data(){
    return{
      formInline: {
        officeName: '',
        officeUid: '',
        activeUid: '',
        oemUid: localStorage.getItem('uid'),
        page: 1,
        number: 10
      },
      types: localStorage.getItem("jurisdiction"),
      eventChildShow: false,
      totalNum: 0, //总页数
      tableData: [],
      listLoading: false,
      detailsData:{}
    }
  },
  watch : {
    eventShow (n) {
      if (n === true) {
        this.queryAgentInfo()
      }
    }
  },
  mounted () {

  },
  methods:{
    // 重置
    reset() {
      this.formInline.officeName = "";
      this.formInline.phone = "";
      this.formInline.payStatus = "";
      this.formInline.oemName = "";
      this.formInline.oEMName = "";
      this.formInline.startDate = "";
      this.formInline.endDate = "";
      this.formInline.officeUid = "";
      this.mechanism = "";
      this.dataTime = [];
    },
    DetailsChild (activeUid,officeUid) {
      this.detailsData = {
        activeUid: activeUid,
        officeUid: officeUid
      }
      this.eventChildShow = true
    },
    queryAgentInfo () {
      this.listLoading = true
      this.formInline.activeUid = this.activeUid
      https.queryAgentInfo(this.formInline).then(res => {
        if (res.data.code !== '0000') {
          this.$notify({title: "错误",message: res.data.msg,type: "error"});
          this.listLoading = false
          return
        }
        console.log(666, res)
        this.tableData = res.data.data
        this.totalNum = res.data.total
        this.listLoading = false
      })
    },
    handleCloses(done) {
      bus.$emit("eventsuccess", false)
    },
    agentInfoExport(){
      
      // window.location.href = `api/active/agentInfo/excel/export?${qs.stringify(this.formInline)}`;
      var _this = this;
      _this
        .axios({
          //axios请求
          method: "post",
          url: "/api/active/agentInfo/excel/export",
          responseType: "blob",
          data: _this.formInline
        })
        .then(function(d) {
          let blob = new Blob([d.data], { type: "application/vnd.ms-excel" });
          /*let objectUrl = URL.createObjectURL(blob);
                        window.location.href = objectUrl;*/

          var date = new Date();

          var year = date.getFullYear();

          var month = date.getMonth() + 1;
          var day = date.getDate();
          var hour = date.getHours();
          var minute = date.getMinutes();
          var second = date.getSeconds();
          date =
            year +
            "" +
            month +
            "" +
            day +
            "" +
            hour +
            "" +
            minute +
            "" +
            second; //'+date+'

          var downloadElement = document.createElement("a");
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "权益列表" + date + ".xls"; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        })
        .catch(function(error) {
          //axios请求失败回调
          console.log(error);
        });
    },
    queryAgentInfos(){
      this.formInline.page = 1; 
      this.queryAgentInfo()
    },
    handleSizeChange(val) {
      this.formInline.number = val; //动态改变
      this.formInline.page = 1;
      this.queryAgentInfo()
    },
    handleCurrentChange(val) {
      this.formInline.page = val; //动态改变
      this.queryAgentInfo()
    },
    eventChildsuccess() {
      bus.$on("eventChildsuccess", res => {
        this.eventChildShow = res;
      });
    },
  }
}
</script>
<style scoped>
.btnExport {
  float: left;
  margin-top: 10px;
  margin-bottom: 10px;
}
.pagination {
  margin-top: 10px;
}
 .sousuojg6_10 {
   width: 100%;
  /* float: left; */
  height: 140px;
  font-size: 16px;
  color: #222;
  box-sizing: border-box;
  background-color: #f6f6f6;
  border-radius: 5px;
}
.shopList {
  width: 100%;
  border-bottom: 1px solid #ccc;
  line-height: 50px;
  font-size: 16px;
  height: 30px;
  margin-bottom: 30px;
}
/deep/ .el-form--inline .el-form-item{
  margin-right: 0;
}
.boxer {
  /* padding-right: 10px; */
  float: right;
}
.boxer > .el-button{
  width: 100px;
  float: right;
}
</style>
